package com.tianbing.adlottery.controller.systemadmin;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import com.fengniu.ejb.business.ISiteBusiness;
import com.fengniu.ejb.business.IUserBusiness;
import com.fengniu.ejb.dao.ICategoryDao;
import com.fengniu.ejb.dao.IContentDao;
import com.fengniu.ejb.dao.ISiteDao;
import com.fengniu.ejb.dao.ISystemDao;
import com.fengniu.ejb.dao.IUserDao;
import com.fengniu.ejb.entity.Content;
import com.fengniu.ejb.entity.Site;
import com.fengniu.ejb.entity.User;
import com.tianbing.adlottery.global.IConstants;
import com.tianbing.adlottery.helper.SkinUtils;
import com.tianbing.adlottery.util.UtilDataGroup;

/**
 * MultiActionController for the image list/upload UI.
 * 
 * @author Conan Chen
 * @since 2006.12.15
 */
public class SystemAdminController extends MultiActionController {
	Logger logger = Logger.getLogger(SystemAdminController.class.getName());

	private String listProductView;

	private String listStoreView;

	private String listCompanyView;

	private String listUserView;

	private IUserBusiness userBusiness;

	private ISiteBusiness siteBusiness;

	private ISiteDao siteDao;

	private IUserDao userDao;

	private IContentDao contentDao;

	private ICategoryDao categoryDao;

	private SkinUtils skinUtils;

	private ISystemDao systemDao;

	public ModelAndView listProduct(HttpServletRequest request, HttpServletResponse response) throws Exception {

		ModelAndView mv = new ModelAndView(listProductView);
		Map<String, Object> model = mv.getModel();

		// get all product
		Integer pgno = ServletRequestUtils.getIntParameter(request, "pgno", 0);
		if (pgno < 0)
			pgno = 0;
		Integer pgsize = ServletRequestUtils.getIntParameter(request, "pgsize", 30);
		if (pgsize < 0)
			pgsize = 30;
		String odby = ServletRequestUtils.getStringParameter(request, "odby", "");

		List<Content> contentList = contentDao.getListContentBy(null, null, null, pgno, pgsize, odby);
		Long totalCount = contentDao.getCountContentBy(null, null, null);
		List contentGroupList = UtilDataGroup.makeGroups(contentList, 0, 6);

		model.put("contentGroupList", contentGroupList);
		model = skinUtils.preparePageFooter(request, contentList, totalCount, pgno, pgsize, model);

		return mv;
	}

	public ModelAndView listStore(HttpServletRequest request, HttpServletResponse response) throws Exception {

		ModelAndView mv = new ModelAndView(listStoreView);
		Map<String, Object> model = mv.getModel();

		// get all company
		Integer pgno = ServletRequestUtils.getIntParameter(request, "pgno", 0);
		if (pgno < 0)
			pgno = 0;
		Integer pgsize = ServletRequestUtils.getIntParameter(request, "pgsize", 30);
		if (pgsize < 0)
			pgsize = 30;
		String odby = ServletRequestUtils.getStringParameter(request, "odby", "");
		Integer[] siteTypeIds = { IConstants.Type.SITE_STORE.getId() };

		List<Site> siteList = siteDao.getListSiteBy(siteTypeIds, null, pgno, pgsize, odby);
		Long totalCount = siteDao.getCountSiteBy(siteTypeIds, null);
		List siteGroupList = UtilDataGroup.makeGroups(siteList, 0, 6);

		model.put("siteGroupList", siteGroupList);
		model = skinUtils.preparePageFooter(request, siteList, totalCount, pgno, pgsize, model);

		return mv;
	}

	public ModelAndView listCompany(HttpServletRequest request, HttpServletResponse response) throws Exception {

		ModelAndView mv = new ModelAndView(listCompanyView);
		Map<String, Object> model = mv.getModel();

		// get all company
		Integer pgno = ServletRequestUtils.getIntParameter(request, "pgno", 0);
		if (pgno < 0)
			pgno = 0;
		Integer pgsize = ServletRequestUtils.getIntParameter(request, "pgsize", 30);
		if (pgsize < 0)
			pgsize = 30;
		String odby = ServletRequestUtils.getStringParameter(request, "odby", "");
		Integer[] siteTypeIds = { IConstants.Type.SITE_COMPANY.getId() };

		List<Site> siteList = siteDao.getListSiteBy(siteTypeIds, null, pgno, pgsize, odby);
		Long totalCount = siteDao.getCountSiteBy(siteTypeIds, null);
		List siteGroupList = UtilDataGroup.makeGroups(siteList, 0, 6);

		model.put("siteGroupList", siteGroupList);
		model = skinUtils.preparePageFooter(request, siteList, totalCount, pgno, pgsize, model);

		return mv;
	}

	public ModelAndView listUser(HttpServletRequest request, HttpServletResponse response) throws Exception {

		ModelAndView mv = new ModelAndView(listUserView);
		Map<String, Object> model = mv.getModel();

		// get all product
		Integer pgno = ServletRequestUtils.getIntParameter(request, "pgno", 0);
		if (pgno < 0)
			pgno = 0;
		Integer pgsize = ServletRequestUtils.getIntParameter(request, "pgsize", 30);
		if (pgsize < 0)
			pgsize = 30;
		String odby = ServletRequestUtils.getStringParameter(request, "odby", "");

		List<User> userList = userDao.getListUserBy(null, pgno, pgsize, odby);
		Long totalCount = userDao.getCountUserBy(null);
		List userGroupList = UtilDataGroup.makeGroups(userList, 0, 6);

		model.put("userGroupList", userGroupList);
		model = skinUtils.preparePageFooter(request, userList, totalCount, pgno, pgsize, model);

		return mv;
	}

	public void setCategoryDao(ICategoryDao categoryDao) {
		this.categoryDao = categoryDao;
	}

	public void setContentDao(IContentDao contentDao) {
		this.contentDao = contentDao;
	}

	public void setListCompanyView(String listCompanyView) {
		this.listCompanyView = listCompanyView;
	}

	public void setListProductView(String listProductView) {
		this.listProductView = listProductView;
	}

	public void setListStoreView(String listStoreView) {
		this.listStoreView = listStoreView;
	}

	public void setListUserView(String listUserView) {
		this.listUserView = listUserView;
	}

	public void setLogger(Logger logger) {
		this.logger = logger;
	}

	public void setSiteBusiness(ISiteBusiness siteBusiness) {
		this.siteBusiness = siteBusiness;
	}

	public void setSiteDao(ISiteDao siteDao) {
		this.siteDao = siteDao;
	}

	public void setSkinUtils(SkinUtils skinUtils) {
		this.skinUtils = skinUtils;
	}

	public void setSystemDao(ISystemDao systemDao) {
		this.systemDao = systemDao;
	}

	public void setUserBusiness(IUserBusiness userBusiness) {
		this.userBusiness = userBusiness;
	}

	public void setUserDao(IUserDao userDao) {
		this.userDao = userDao;
	}

}
